/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;

import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.util.StringUtils;
import cn.qidisoft.edu.hzjt.constants.Constant;
import cn.qidisoft.edu.hzjt.model.Grade;
import cn.qidisoft.edu.hzjt.model.Semester;
import cn.qidisoft.edu.hzjt.model.Specialty;
import cn.qidisoft.edu.hzjt.model.SysCode;
import cn.qidisoft.edu.hzjt.model.UserEmployee;
import cn.qidisoft.edu.hzjt.service.ICommonService;

/**
 * 
 * @author hxq
 * @version $Id: BaseController.java, v 0.1 2018年9月28日 下午4:46:08 hxq Exp $
 */
@Component
public class BaseController {
  private static Map<String, Object> parameterBox = null;

  @Autowired
  private ICommonService commonService;
  // 获得所有标准代码信息
  public static List<SysCode> codeList = new ArrayList<SysCode>();
  // 获得学期集合信息
  public static List<Semester> semesterList = new ArrayList<Semester>();
  // 通过代码获得标准信息
  public static List<SysCode> sysCodeList = new ArrayList<SysCode>();
  // 获得年级集合信息
  public static List<Grade> gradeList = new ArrayList<Grade>();
  // 获得专业集合信息
  public static List<Specialty> specialityList = new ArrayList<>();
  // 获得名族信息
  public static List<SysCode> nationList = new ArrayList<SysCode>();
  // 获得学年信息
  public static List<Semester> yearList = new ArrayList<Semester>();
  // 通过学年获得学期信息
  public static List<Semester> yearBySemesterList = new ArrayList<Semester>();
  public static final String STUDENT_BZRID = "bzrid";
  // 是否是班主任
  public static final String TEACHER_IS_MANAGER = "ismanager";



  @PostConstruct
  public void init() {
    parameterBox = new HashMap<String, Object>();
    codeList = commonService.queryAllBySysCode();
  }

  /**
   * 设置参数
   * 
   * @param key
   * @param value
   */
  public static void setParameter(String key, Object value) {
    parameterBox.put(key, value);
  }

  /**
   * 获取参数
   * 
   * @param key
   * @return
   */
  public static String getParameter(String key) {
    if (parameterBox.get(key) != null) {
      return (String) parameterBox.get(key);
    } else {
      return null;
    }
  }

  /**
   * 删除参数
   * 
   * @param key
   */
  public static void removeParameter(String key) {
    if (parameterBox.get(key) != null) {
      parameterBox.remove(key);
    }
  }

  /**
   * 清空参数
   */
  @PreDestroy
  public void clearParameterBox() {
    parameterBox.clear();
  }

  /**
   * 设置排序条件
   * 
   * @param request
   * @param page
   */
  public static void setSortByColumn(HttpServletRequest request, String OrderDefaultColumn,
      String OrderDefaultFlag) {
    String orderField =
        request.getParameter("sort") == null ? OrderDefaultColumn : request.getParameter("sort");
    String orderFlag =
        request.getParameter("order") == null ? OrderDefaultFlag : request.getParameter("order");
    request.setAttribute("orderField", orderField);
    request.setAttribute("orderFlag", orderFlag);
  }

  /**
   * 通过代码获得标准代码信息
   * 
   * @param categoryCode
   * @return
   */
  public static List<SysCode> getSysCodeList(String categoryCode) {
    SysCode standardCode = null;
    List<SysCode> sysCodeList2 = new ArrayList<>();
    if (!codeList.isEmpty()) {
      for (SysCode sysCode : codeList) {
        if (categoryCode.equals(sysCode.getCategory())) {
          sysCodeList2.add(sysCode);
        }
      }
    } else {
      return null;
    }
    return sysCodeList2;
  }


  /**
   * 获得分页配置信息
   * 
   * @param page
   * @return
   */
  public static Page getPage(String currentPage, String pageSize, HttpServletRequest request,
      String orderDefaultColumn, String OrderDefaultFlag) {
    Page page = new Page();
    page.setCurrPage(currentPage == null ? 0 : Integer.valueOf(currentPage));
    page.setPageSize(pageSize == null ? Constant.PageSize : Integer.valueOf(pageSize));
    setSortByColumn(request, orderDefaultColumn, OrderDefaultFlag);
    return page;
  }

  /**
   * 获得政治面貌标准信息
   * 
   * @param model
   * @param commonService
   */
  public static void getCodeList(ModelMap model, ICommonService commonService, String sysCode) {
    sysCodeList = (List<SysCode>) BaseController.getSysCodeList(sysCode);
    model.put("sysCodeList", sysCodeList);
  }

  /**
   * 获得民族标准信息
   * 
   * @param model
   * @param commonService
   * @param sysCode
   */
  public static void getNationList(ModelMap model, ICommonService commonService, String sysCode) {
    nationList = (List<SysCode>) BaseController.getSysCodeList(sysCode);
    model.put("nationList", nationList);
  }

  /**
   * 获得学期信息集合
   * 
   * @param model
   * @param commonService
   */
  public static void getSemesterList(ModelMap model, ICommonService commonService) {
    semesterList = commonService.queryAllBySemester();
    model.put("semesterList", semesterList);
  }

  /**
   * 获得当前学期信息
   * 
   * @param model
   * @param commonService
   */
  public static void getSemester(ModelMap model, ICommonService commonService) {
    Semester semester = new Semester();
    semester.setCurrentxq(true);
    semester = commonService.queryBySemester(semester);
    model.put("semester", semester);
  }

  /**
   * 获得年级集合信息
   * 
   * @param model
   * @param commonService
   */
  public static void getGradeList(ModelMap model, ICommonService commonService) {
    Grade grade = new Grade();
    if (model.get(STUDENT_BZRID) != null) {
      grade.setBzrid((Integer) model.get(STUDENT_BZRID));
    }
    gradeList = commonService.queryAllByGrade(grade);
    model.put("gradeList", gradeList);
  }

  /**
   * 获得专业集合信息
   * 
   * @param model
   * @param commonService
   */
  public static void getSpecialityList(ModelMap model, ICommonService commonService) {
    Specialty specialty = new Specialty();
    if (model.get(STUDENT_BZRID) != null) {
      specialty.setBzrid((Integer) model.get(STUDENT_BZRID));
    }
    specialityList = commonService.queryAllBySpecialty(specialty);
    model.put("specialityList", specialityList);
  }

  /**
   * 获得学年信息
   * 
   * @param model
   * @param commonService
   */
  public static List<Semester> getYearList(ICommonService commonService) {
    yearList = commonService.queryAllByYear();
    return yearList;
  }

  /**
   * 通过学年获得学期信息
   * 
   * @param model
   * @param commonService
   * @return
   */
  public static List<Semester> getYearBySemesterList(Integer year, ICommonService commonService) {
    if (year != null) {
      Semester semester = new Semester();
      semester.setXnd(year);
      yearBySemesterList = commonService.queryYearBySemester(semester);
      return yearBySemesterList;
    } else {
      return null;
    }

  }

  /**
   * 获取当前用户对应的班级信息
   * 
   * @param emid
   * @param model
   */
  public static void getClassListAndBzrid(HttpServletRequest request, ModelMap model,
      ICommonService commonService, Integer loginType) {
    switch (loginType) {
      case 1: // 学生端
        model.put(TEACHER_IS_MANAGER, Constant.IS_STUDENT);
        model.put(STUDENT_BZRID, null);
        break;
      case 2: // 老师端
        String bzr = request.getParameter("bzr");
        UserEmployee userEmployee =
            (UserEmployee) request.getSession().getAttribute(Constant.LOGIN_CONSOLE_USER);
        if (!StringUtils.isNullValue(bzr) && Constant.IS_BZR.equals(bzr)) { // 标识是班主任
          model.put(TEACHER_IS_MANAGER, Constant.IS_BZR);
          model.put(STUDENT_BZRID, userEmployee.getEmid());
        } else {
          model.put(TEACHER_IS_MANAGER, Constant.IS_MANAGER);
          model.put(STUDENT_BZRID, null);
        }
        break;
      default:
        break;
    }



    /** 获取当前班主任ID **/
    // if (model.get(BaseController.STUDENT_BZRID) != null) {
    // student.setBzrid((Integer) model.get(BaseController.STUDENT_BZRID));
    // }
    // List<StudentClass> classList = commonService.queryGradeSpecialtyByClass(student);
    // model.put("classList", classList);
  }

  public static Map<String, Object> getParameterBox() {
    return parameterBox;
  }

  public static void setParameterBox(Map<String, Object> parameterBox) {
    BaseController.parameterBox = parameterBox;
  }

  public static List<SysCode> getCodeList() {
    return codeList;
  }

  public static void setCodeList(List<SysCode> codeList) {
    BaseController.codeList = codeList;
  }

  public static List<Semester> getSemesterList() {
    return semesterList;
  }

  public static void setSemesterList(List<Semester> semesterList) {
    BaseController.semesterList = semesterList;
  }


  public static List<SysCode> getSysCodeList() {
    return sysCodeList;
  }

  public static void setSysCodeList(List<SysCode> sysCodeList) {
    BaseController.sysCodeList = sysCodeList;
  }

  public static List<Grade> getGradeList() {
    return gradeList;
  }

  public static void setGradeList(List<Grade> gradeList) {
    BaseController.gradeList = gradeList;
  }

  public static List<Specialty> getSpecialityList() {
    return specialityList;
  }

  public static void setSpecialityList(List<Specialty> specialityList) {
    BaseController.specialityList = specialityList;
  }

  public static List<SysCode> getNationList() {
    return nationList;
  }

  public static void setNationList(List<SysCode> nationList) {
    BaseController.nationList = nationList;
  }

  public static List<Semester> getYearList() {
    return yearList;
  }

  public static void setYearList(List<Semester> yearList) {
    BaseController.yearList = yearList;
  }

  public static List<Semester> getYearBySemesterList() {
    return yearBySemesterList;
  }

  public static void setYearBySemesterList(List<Semester> yearBySemesterList) {
    BaseController.yearBySemesterList = yearBySemesterList;
  }

  /*
   * 
   * ?uid=123&type=s/t
   * 
   * CurrentLoginedUser CurrentLoginedStudent
   * 
   * 老师登录：Sys_Users 左关联 BD_Employee 需要读取的信息：职工ID、职工号、姓名
   * 
   * select UID,Sys_Users.UserId,EMID,ZGH,XM,XB from Sys_Users left outer join BD_Employee on
   * BD_Employee.UserId=Sys_Users.UserId
   * 
   * 
   * 学生登录：Stu_Student select XSID,XH,Stu_Student.XM,Stu_Student.XB,
   * BD_Class.BJID,BJMC,BD_Class.NJID,NJMC,BD_Class.ZYID,ZYMC,BD_Specialty.YXID,YXMC,
   * BZRID,BD_Employee.XM as BZRXM,ZGH as BZRGH from Stu_Student inner join BD_Class on
   * BD_Class.BJID=Stu_Student.BJID inner join BD_Specialty on BD_Specialty.ZYID=BD_Class.ZYID inner
   * join BD_Grade on BD_Grade.NJID=BD_Class.NJID left outer join BD_Employee on
   * BD_Employee.EMID=BD_Class.BZRID left outer join BD_Faculty on BD_Faculty.YXID=BD_Specialty.YXID
   * 
   * 
   * 取权限： select CellId,IsAdmin,CanCreate,CanDelete,CanModify from Sys_Priority where UID=1 union
   * select CellId,IsAdmin,CanCreate,CanDelete,CanModify from Sys_Priority inner join Sys_UserRole
   * on Sys_UserRole.RoleID=Sys_Priority.RoleId where Sys_UserRole.UID=1
   * 
   * 
   * 选择数据的窗口： 选择职工 选择学生
   * 
   * 
   */
}
